
(function(){
	
	// 领取优惠券
	$('.yhq-wrapper .col').click(function(){
		
		var cardId = $(this).attr("data-id");
		
		var yhqImg = $(this).find('.photo-img').find('img').attr('src');
		var yhqName = $(this).find('.photo-info').find('.yhq-name').text();
		var yhqInfo = $(this).find('.photo-info').find('.yhq-info').text();
		var yhqDate = $(this).find('.photo-info').find('.yhq-last-data').text();
		var yhqAlert = $('.yhq-alert-wrapper');
		yhqAlert.removeClass('hide');
		yhqAlert.find('#photo-img').find('img').attr('src',yhqImg);
		yhqAlert.find('#yhq-name').text(yhqName);
		yhqAlert.find('#yhq-info').text(yhqInfo);
		yhqAlert.find('#yhq-info').attr("data-id",cardId);
		
		yhqAlert.find('#yhq-last-data').text(yhqDate);
	});
	$('.alert-wrapper .icon-close').click(function(){
		$(this).parent().parent().addClass('hide');
		
	});
	//立即兑换弹框
	$('#duihuan').click(function(){
		$('.duihuan-alert-wrapper').removeClass('hide');
		
	});
	$('#jifen').click(function(){
		$('.jifen-alert-wrapper').removeClass('hide');
		
	});
	//上传照片
	$('.upload-empty').click(function(){
		$(this).before(function(){
			return '<div class="upload upload-img" style="background-image:url(./images/photo-img.png)"></div>';
		});
	})
})();
// 填写信息
function info(){
	
	var cardId = $("#yhq-info").attr("data-id");
	
	ajaxCard(cardId);

};
function yhqInfo(){
	
	var cardId = $("#yhq-info").attr("data-id");
	
	saveInfo(cardId);
	
};

//不打断alert
function warn(msg,opt,left,top){
	if(opt){
	 	var obj = $("#"+opt);
	}
 	new Toast({context:$('body'),message:msg},obj,left,top).show();
}
var Toast = function(config,obj,left,top){
	this.context = config.context==null?$('body'):config.context;//上下文
	this.message = config.message;//显示内容
	this.time = config.time==null?5000:config.time;//持续时间
	this.left = config.left;//距容器左边的距离
	this.top = ($(document).height()/4)*3;//距容器上方的距离
	if(obj){ 
	this.left = obj.offset().left + left;
	this.top = obj.offset().top + top;
	}
	this.init();
}
var msgEntity;
Toast.prototype = {
	//初始化显示的位置内容等
	init : function(){
		$("#toastMessage").remove();
		//设置消息体
		var msgDIV = new Array();
		msgDIV.push('<div id="toastMessage">');
		msgDIV.push('<span>'+this.message+'</span>');
		msgDIV.push('</div>');
		msgEntity = $(msgDIV.join('')).appendTo(this.context);
		//设置消息样式
		var left = this.left == null ? this.context.width()/2-17-msgEntity.find('span').width()/2 : this.left;
		var top = this.top == null ? '20px' : this.top;
		msgEntity.css({position:'absolute',top:top,'z-index':'9999',left:left,'background-color':'#2a70e6',color:'white','font-size':'14px',padding:'12px',margin:'5px','border-radius':'4px','-moz-border-radius':'4px','-webkit-border-radius':'4px','font-family':'微软雅黑'});
		//msgEntity.addClass(".toast");
		msgEntity.hide();
 	},
	//显示动画
	show :function(){
		msgEntity.fadeIn(this.time/2);
		msgEntity.fadeOut(this.time/2);
	}
}

// index
$(document).ready(function(){
	
	//弹幕过后进入主页
	var lead = setTimeout(function(){
			$('.lead_page').fadeOut();
			$('.gongneng-box').removeClass('hide');
			$('.banner-img-box,.main_image').height($('.banner-img').height() )
			mygongneng();
		},3500);
	$('.lead_page a').click(function(e){
		e.preventDefault();
		clearTimeout(lead);
		$('.lead_page').fadeOut();
		$('.gongneng-box').removeClass('hide');
		$('.banner-img-box,.main_image').height($('.banner-img').height() )
		mygongneng();
	});
	
	$('#red-bag').click(function(){
		$('.zj-wrapper').removeClass('hide');
		// $('.bz-wrapper').removeClass('hide');
	})
	
	//保存轮播的div高跟图一样高
	$('.banner-img-box,.main_image').height($('.banner-img').height() )
	//轮播
	function mybaners(){
		$dragBln = false;
		if($('.main_image').length){
			var mainImage = $('.main_image');
			mainImage.touchSlider({
				flexible : true,
				speed : 200,
				btn_prev : $("#btn_prev"),
				btn_next : $("#btn_next"),
				paging : $(".flicking_con a"),
				counter : function (e){
					$(".flicking_con a").removeClass("on").eq(e.current-1).addClass("on");
				}
			});
			
			mainImage.bind("mousedown", function() {
				$dragBln = false;
			});
			
			mainImage.bind("dragstart", function() {
				$dragBln = true;
			});
			
			$(".main_image a").click(function(){
				if($dragBln) {
					return false;
				}
			});
			
			timer = setInterval(function(){
				$("#btn_next").click();
			}, 5000);
			
			$(".main_visual").hover(function(){
				clearInterval(timer);
			},function(){
				timer = setInterval(function(){
					$("#btn_next").click();
				},5000);
			});
			
			mainImage.bind("touchstart",function(){
				clearInterval(timer);
			}).bind("touchend", function(){
				timer = setInterval(function(){
					$("#btn_next").click();
				}, 5000);
			});
		}
	}
	
	function mygongneng(){
		var h=$('.gongneng1').height();
		$('.gongneng3').css('top',h+8)
		$('.gongneng4').css('top',h+8)
	}
	
	mybaners();
	mygongneng();
	$(window).resize(function() {
	$('.banner-img-box,.main_image').height($('.banner-img').height() )
	  mygongneng();
	});
	
});